package com.tanhua.server.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.service.VoiceUploadService;
import com.tanhua.common.vo.VoiceUploadResult;
import com.tanhua.dubbo.server.api.PeachBlossomApi;

import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("peachblossom")
@RestController
public class VioceUploadController {

    @Autowired
    private VoiceUploadService voiceUploadService;

    @Reference(version = "1.0.0")
    private PeachBlossomApi peachBlossomApi;

    @Autowired
    private UserService userService;

    @PostMapping
    @ResponseBody
    public VoiceUploadResult upload(@RequestParam("soundFile") MultipartFile multipartFile,
                                    @RequestHeader("Authorization") String token) {

        User user = userService.queryUserByToken(token);
        try {
            VoiceUploadResult upload = this.voiceUploadService.upload(multipartFile);

            this.peachBlossomApi.saveVoice(user.getId(), upload.getName());

            return upload;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
